/*
 * Copyright (C) 2011 by Fabien Barbero
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.api.photosets;

import com.flickr.api.AbstractFlickrService;
import com.flickr.api.ApiKey;
import com.flickr.api.CommandArguments;
import com.flickr.api.FlickrServiceException;
import com.flickr.api.LocalCache;
import com.flickr.api.Paginated;
import com.flickr.api.VoidResponse;
import com.flickr.api.auth.AuthenticationService;
import com.flickr.api.people.User;
import com.flickr.api.photos.Photo;
import com.flickr.api.photos.PhotoContext;
import com.flickr.utils.FlickrUtils;
import com.google.inject.Inject;
import java.util.List;

/**
 *
 * @author Fabien Barbero
 */
public class PhotosetsServiceImpl extends AbstractFlickrService implements PhotosetsService {
    
    private final ApiKey apiKey;
    private final AuthenticationService authService;
    private final LocalCache cache;
    
    @Inject
    public PhotosetsServiceImpl(ApiKey apiKey, AuthenticationService authService, LocalCache cache) {
        this.apiKey = apiKey;
        this.authService = authService;
        this.cache = cache;
    }

    @Override
    public void addPhotoToPhotoset(Photoset photoset, Photo photo) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.addPhoto", apiKey);
        args.put("photo_id", photo.getId());
        args.put("photoset_id", photoset.getId());
        doPost(args, VoidResponse.class);
    }

    @Override
    public Photoset createPhotoset(String title, String description, Photo primaryPhoto) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.create", apiKey);
        args.put("title", title);
        args.put("description", description);
        args.put("primary_photo_id", primaryPhoto.getId());
        Photoset set = doGet(args, PhotosetResponse.class).getPhotoset();
        set.setCache(cache);
        return set;
    }

    @Override
    public void removePhotoset(Photoset photoset) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.delete", apiKey);
        args.put("photoset_id", photoset.getId());
        doPost(args, VoidResponse.class);
    }

    @Override
    public void editPhotoset(Photoset photoset, String title, String description) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.editMeta", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("title", title);
        args.put("description", description);
        doPost(args, VoidResponse.class);
    }

    @Override
    public void editPhotos(Photoset photoset, Photo primaryPhoto, List<Photo> photos) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.editPhotos", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("primary_photo_id", primaryPhoto.getId());
        args.put("description", FlickrUtils.concat(photos, ","));
        doPost(args, VoidResponse.class);
    }

    @Override
    public PhotoContext getContext(Photoset photoset, Photo photo) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.getContext", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("photo_id", photo.getId());
        return doGet(args, PhotoContext.class);
    }

    @Override
    public Photoset getPhotosetById(String id) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.getInfo", apiKey);
        args.put("photoset_id", id);
        Photoset set = doGet(args, PhotosetResponse.class).getPhotoset();
        set.setCache(cache);
        return set;
    }

    @Override
    public List<Photoset> getPhotosets(User user) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.getList", apiKey);
        args.put("user_id", user.getId());
        List<Photoset> sets = doGet(args, PhotosetsResponse.class).getPhotosets();
        addCacheToPhotosets(sets);
        return sets;
    }

    @Override
    public Paginated<Photo> getPhotos(Photoset photoset, int perPage, int page) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.getList", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("per_page", perPage);
        args.put("page", page);
        Paginated<Photo> sets = doGet(args, PaginatedPhotosResponse.class).getPhotos();
        return sets;
    }

    @Override
    public void orderPhotosets(List<Photoset> photosets) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.orderSets", apiKey);
        args.put("photoset_ids", FlickrUtils.concat(photosets, ","));
        doPost(args, VoidResponse.class);
    }

    @Override
    public void removePhotoFromPhotoset(Photoset photoset, Photo photo) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.removePhoto", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("photo_id", photo.getId());
        doPost(args, VoidResponse.class);
    }

    @Override
    public void removePhotosFromPhotoset(Photoset photoset, List<Photo> photos) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.removePhotos", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("photo_ids", FlickrUtils.concat(photos, ","));
        doPost(args, VoidResponse.class);
    }

    @Override
    public void reorderPhotos(Photoset photoset, List<Photo> photos) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.reorderPhotos", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("photo_ids", FlickrUtils.concat(photos, ","));
        doPost(args, VoidResponse.class);
    }

    @Override
    public void setPhotosetPrimaryPhoto(Photoset photoset, Photo primaryPhoto) throws FlickrServiceException {
        CommandArguments args = new CommandArguments("flickr.photosets.setPrimaryPhoto", apiKey);
        args.put("photoset_id", photoset.getId());
        args.put("photo_id", primaryPhoto.getId());
        doPost(args, VoidResponse.class);
    }
    
    private void addCacheToPhotosets(List<Photoset> sets) {
        for(Photoset set : sets) {
            set.setCache(cache);
        }
    }
    
}
